#include <gdk/gdkkeysyms.h>

#include "sms.h"

#define MAX_ICON 2
#define TEST_DOWNLOAD_FILE "http://www.xiyuit.com/picture/article/49/48264.jpg"

typedef struct _TestLayout_2
{
	Layout *layout;
	int cur_icon;
	GooCanvasItem *icon[2];
	
	int cur_bt;
	Button *button[3];

	GooCanvasItem *text;
	Downloader *downloader;
	Exec *exec;
}TestLayout_2;


static gboolean test2_exec_finish(Exec *exec,gpointer data)
{
	TestLayout_2 *test_layout=data;
	exec_destroy(test_layout->exec);
	test_layout->exec=NULL;
	g_object_set(test_layout->text,"text","Exit gqview...",NULL);
}

static void test2_exec_create(Layout *layout)
{
	printf("~~~~~~~ %s\n",__FUNCTION__);
	char *argv[]={"gqview","-f","/usr/share/pixmaps",NULL};
	TestLayout_2 *test_layout=(TestLayout_2 *)layout->private;
	test_layout->exec=exec_create("gqview",3,argv,FALSE,
								  test2_exec_finish,test_layout);
	if(!test_layout->exec)
		g_object_set(test_layout->text,"text","Load gqview error",NULL);
	else
		g_object_set(test_layout->text,"text","Loading gqview...",NULL);
}

/////////////////////////////////////////////////////////////////////////
	
static gboolean test2_download_finish_cb(Downloader *downloader,gpointer data)
{
	char buf[64];
	TestLayout_2 *test_layout=data;

	sprintf(buf,"Download finish.");
	g_object_set(test_layout->text,"text",buf,NULL);
	
	downloader_delete(test_layout->downloader);
	test_layout->downloader=NULL;
	return TRUE;
}

static gboolean test2_download_error_cb(Downloader *downloader,gpointer data)
{
	char buf[64];
	TestLayout_2 *test_layout=data;

	sprintf(buf,"Download error :  %d\%",downloader->err_id);
	g_object_set(test_layout->text,"text",buf,NULL);
	downloader_delete(test_layout->downloader);
	test_layout->downloader=NULL;
	
	return TRUE;
}

static gboolean test2_download_progress_cb(Downloader *downloader,gpointer data)
{
	char buf[64];
	TestLayout_2 *test_layout=data;

	sprintf(buf,"Downloading : %3d%%",downloader->download_progress);

	g_object_set(test_layout->text,"text",buf,NULL);
	
	return TRUE;
}

static void test2_download_file(Layout *layout)
{
	TestLayout_2 *test_layout=(TestLayout_2 *)layout->private;
	test_layout->downloader=downloader_new(TEST_DOWNLOAD_FILE,"/tmp/sms-test2-download",-1,test_layout);
	if(!test_layout->downloader)
	{
		fprintf(stderr,"%s:Creat downloader error\n",__FUNCTION__);
	}
	
	downloader_set_func(test_layout->downloader,"download-finish",test2_download_finish_cb);
	downloader_set_func(test_layout->downloader,"download-error",test2_download_error_cb);
	downloader_set_func(test_layout->downloader,"download-progress",test2_download_progress_cb);

	downloader_run(test_layout->downloader);
	g_object_set(test_layout->text,"text","Downloading :   0%",NULL);
	
}

//////////////////////////////////////////////////
//button cb
static gboolean button_0_cb(Button *Button,gpointer data)
{
	Layout *layout=data;
	TestLayout_2 *test_layout=layout->private;
	g_object_set(test_layout->text,"text","You press button 0\n",NULL);
	return TRUE;
}

static gboolean button_1_cb(Button *Button,gpointer data)
{
	Layout *layout=data;
	TestLayout_2 *test_layout=layout->private;
	g_object_set(test_layout->text,"text","You press button 1 start ani\n",NULL);
	goo_canvas_item_animate(test_layout->text,400,300,1.5,60,TRUE,500,50,GOO_CANVAS_ANIMATE_BOUNCE);
	return TRUE;
}

static gboolean button_2_cb(Button *Button,gpointer data)
{
	Layout *layout=data;
	TestLayout_2 *test_layout=layout->private;
	g_object_set(test_layout->text,"text","You press button 2 stop ani\n",NULL);
	goo_canvas_item_stop_animation(test_layout->text);
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////

static gboolean test_layout_init(Layout *layout,gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	
	TestLayout_2 *test_layout=(TestLayout_2 *)g_malloc0(sizeof(TestLayout_2)+1);
	layout->private=(void *)test_layout;

	layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

	test_layout->icon[0]=goo_canvas_rect_new(layout->cavas,150,50,48,48,
											 "stroke-pattern", NULL,
											"fill-color", "red",
   											NULL);
	test_layout->icon[1]=goo_canvas_rect_new(layout->cavas,220,50,48,48,
											 "stroke-pattern", NULL,
											"fill-color", "yellow",
   											NULL);

	g_object_set (test_layout->icon[test_layout->cur_icon], "stroke-color", "black", NULL);
	
	test_layout->text=goo_canvas_text_new(layout->cavas,"",50,200,-1,GTK_ANCHOR_WEST,
										  "font", "Sans Bold 32",
											NULL);
	SMS *sms=layout->sms;
	test_layout->button[0]=button_new_with_text(layout->cavas,150,100,sms->theme,"button 0");
	button_set_func(test_layout->button[0],"clicked",button_0_cb,layout);
	
	test_layout->button[1]=button_new_with_text(layout->cavas,150,200,sms->theme,"button 1");
	button_set_func(test_layout->button[1],"clicked",button_1_cb,layout);
	
	test_layout->button[2]=button_new_with_text(layout->cavas,150,300,sms->theme,"button 2");
	button_set_func(test_layout->button[2],"clicked",button_2_cb,layout);
	
	button_set_focus(test_layout->button[2],TRUE);
	test_layout->cur_bt=2;
	return TRUE;
}

static gboolean test_layout_clear(Layout *layout,gpointer data)
{
	TestLayout_2 *test_layout;
	int i;

	printf("call %s\n",__FUNCTION__);

	test_layout=layout->private;

	for(i=0;i<MAX_ICON;i++)
		g_object_unref(test_layout->icon[i]);	

	g_free(test_layout);
}

static gboolean test_layout_key_press(Layout * layout,GdkEventKey *event)
{
	TestLayout_2 *test_layout;

	test_layout=layout->private;

	if(event->keyval==GDK_Left)
	{
		if(test_layout->cur_icon>0)
		{
			g_object_set (test_layout->icon[test_layout->cur_icon], "stroke-pattern", NULL, NULL);
			test_layout->cur_icon--;
			g_object_set (test_layout->icon[test_layout->cur_icon], "stroke-color", "black", NULL);
		}
		return TRUE;
	}
	else if(event->keyval==GDK_Right)
	{
		if(test_layout->cur_icon<MAX_ICON-1)
		{
			g_object_set (test_layout->icon[test_layout->cur_icon], "stroke-pattern", NULL, NULL);
			test_layout->cur_icon++;
			g_object_set (test_layout->icon[test_layout->cur_icon], "stroke-color", "black", NULL);
		}
		return TRUE;
	}
	if(event->keyval==GDK_Up)
	{
		if(test_layout->cur_bt>0)
		{
			button_set_focus(test_layout->button[test_layout->cur_bt],FALSE);
			test_layout->cur_bt--;
			button_set_focus(test_layout->button[test_layout->cur_bt],TRUE);
			
		}
		return TRUE;
	}
	else if(event->keyval==GDK_Down)
	{
		if(test_layout->cur_bt<3-1)
		{
			button_set_focus(test_layout->button[test_layout->cur_bt],FALSE);
			test_layout->cur_bt++;
			button_set_focus(test_layout->button[test_layout->cur_bt],TRUE);
		}
		return TRUE;
	}
	else if(event->keyval==GDK_Escape)
	{		
		sms_change_win_to(layout->sms,LAYOUT_WIN_MAIN);
		return TRUE;
	}
	else if(event->keyval==GDK_Return)
	{
		if(test_layout->cur_icon==0)
		{
#if 0
			if(test_layout->downloader==NULL)
				test2_download_file(layout);
			else
			{
				downloader_stop(test_layout->downloader,TRUE);
				test_layout->downloader=NULL;
			}
#else
			return button_click(test_layout->button[test_layout->cur_bt]);
#endif
		}
		else if(test_layout->cur_icon==1)
		{
			if(test_layout->exec==NULL)
				test2_exec_create(layout);
			else
			{
				exec_destroy(test_layout->exec);
				test_layout->exec=NULL;
			}
		}
		return TRUE;
	}
	printf("%s : propagate the event\n",__FUNCTION__);
	return FALSE;
}

Layout *test2_layout_new(GooCanvasItem * parent,gpointer sms)
{
	Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN,LAYOUT_WIN_TEST2,
							   "test2",sms);

	layout_set_func(layout,"init",test_layout_init);
	layout_set_func(layout,"clear",test_layout_clear);
	layout_set_func(layout,"key-press",test_layout_key_press);
	return layout;
}
